package com.example.recyclerview1;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;

import org.jetbrains.annotations.NotNull;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.recyclerview);
        //默认垂直线性布局，水平的话把里面的LinearLayoutManager弄成变量，然后对它.setOrientation(RecyclerView.HORIZONTAL)
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        recyclerView.addItemDecoration(new MyDecoration());//添加空隙
        recyclerView.setAdapter(new RecyclerViewAdapter(MainActivity.this));
    }

    //添加空隙，每个item的下面添加一段空隙（说实话，不如直接在item的布局里添加
    class MyDecoration extends RecyclerView.ItemDecoration {
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.set(0,0,0, getResources().getDimensionPixelOffset(R.dimen.dividerHeight));
        }
    }
}